package handler

import (
	"context"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
	"path/filepath"
	"time"
	"zg4/zk1/api/pkg"
	__ "zg4/zk1/api/proto"
	"zg4/zk1/api/request"
)

func AddVideo(c *gin.Context) {
	var req request.AddVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("localhost:8899", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}

	defer conn.Close()
	xx := __.NewVideoClient(conn)
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	file, _ := c.FormFile("file")
	ext := filepath.Ext(file.Filename)

	if file.Size > 20*1024*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "视频大小不能超过20GB",
			"data": nil,
		})
		return
	}
	if ext != ".mp4" && ext != ".mov" {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "视频类型必须为mp4或mov",
			"data": nil,
		})
		return
	}
	filename := time.Now().Format("20060102150405") + ext
	dst := "./static/image/" + filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)

	pkg.Upload(dst, filename)

	video, err := xx.AddVideo(ctx, &__.AddVideoRequest{
		Name:   req.Name,
		Cover:  req.Cover,
		Status: int64(req.Status),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "发布视频成功",
		"data": video,
	})
	return
}

func VideoInfo(c *gin.Context) {
	var req request.VideoInfoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("localhost:8899", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	xx := __.NewVideoClient(conn)
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	video, err := xx.VideoInfo(ctx, &__.VideoInfoRequest{
		VideoId: int64(req.VideoId),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "视频详情展示成功",
		"data": video,
	})
	return
}

func RecommendVideoList(c *gin.Context) {
	var req request.RecommendVideoListReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("localhost:8899", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	xx := __.NewVideoClient(conn)
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	videoList, err := xx.RecommendVideoList(ctx, &__.RecommendVideoListRequest{
		Page: req.Page,
		Size: req.Size,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "推荐视频列表展示成功",
		"data": videoList,
	})
	return
}
